-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4 - part1.py
More file actions
55 lines (45 loc) · 1.48 KB
/
day4 - part1.py
File metadata and controls
55 lines (45 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
class Board:
def __init__(self):
self.grid = []
self.rows = []
self.cols = []
self.coords = []
def draw(self, value):
for i, row in enumerate(self.grid):
if value in row:
r, c = i, row.index(value)
self.rows.append(r)
self.cols.append(c)
self.coords.append((r, c))
return self.checkVictory(value, r, c)
def checkVictory(self, value, row_val, col_val):
if self.rows.count(row_val) == 5 or self.cols.count(col_val) == 5:
_sum = 0
for y, row in enumerate(self.grid):
for x, col in enumerate(row):
if (y, x) not in self.coords:
_sum += col
return _sum * value
return None
file = open("input.txt")
lines = file.readlines()
draw = list(map(int, lines[0].split(",")))
boards = []
# parsing each board
for board_i in range(2, len(lines), 6): # step by step each board
board = Board()
for grid_line in lines[board_i:][:5]:
# parsing each str row to int list
board.grid.append(
[int(grid_line[i : i + 3]) for i in range(0, len(grid_line), 3)]
)
boards.append(board)
# making the draws
for d in draw:
for board in boards:
victory = board.draw(d)
if victory is None:
continue
print(victory)
sys.exit()